/*
 * Items.cpp
 *
 *  Created on: Aug 29, 2013
 *      Author: jason
 */

#include <string>
#include "../../include/Items/Items.hpp"
#include "../../include/GlobalState.hpp"
#include "../../include/Common.hpp"

namespace OForce {

GenericItem::GenericItem(int id) : mId(id),
		mPrice(0),
		mUsable(NOWHERE),
		mTargetType(SELF)
{}

void GenericItem::readObjectData(LuaScript script) {
	mName = script.readString("name");
	mPrice = script.readInt("price");
	mIconFile = script.readString("icon");
	if (!mIconTexture.loadFromFile(mIconFile)) {
		std::cerr << "Error: cannot find icon: " << mIconFile << "\n";
	}
	battleUse = script.ReadFunctionPointer("BattleUse");
	worldUse  = script.ReadFunctionPointer("Use");
}

void GenericItem::setIcon(std::string filename) {
	mIconTexture.loadFromFile(filename);
	mIcon.setTexture(mIconTexture);
}

void GenericItem::setIconPosition(float x, float y) {
	mIcon.setPosition(x,y);
}

Item::Item(int id) : GenericItem(id)
{
	script = GlobalState::Instance()->getItemScript();
	if (script.doesTableExist(id) == false) {
		std::cerr << "Invalid item with key: " << id << "\n";
	} else {
		script.openTable(id);
		readObjectData(script);
		script.closeTable();
	}
}

Item::~Item()
{
}

void Item::draw() {
	Window.draw(mIcon);
}

}// namespace
